package com.bobi.tfca.ui.member

import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MemberAPI
import com.bobi.tfca.network.api.MessageAPI
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class GroupAddMemFragmentVM : ViewModel() {

    val members by lazy {
        MutableLiveData(ArrayList<MemberResponse>())
    }

    fun addMember(
        publicViewModel: PublicViewModel,
        groupId: Int,
        choseMemberIdList: List<Int>,
        fragment: GroupAddMemberFragment
    ) {
        publicViewModel.apply {
            request(MessageAPI::class.java).addMember2Group(groupId, choseMemberIdList)
                .getResponse {
                    it.collect {
                        when (it) {
                            is ApiResponse.Error -> {
                                LogUtil.e(it.errMsg)
                            }

                            ApiResponse.Loading -> LogUtil.e("Loading")
                            is ApiResponse.Success -> {
                                withContext(Dispatchers.Main) {
                                    val code = it.data?.code
                                    if (code == 200) {
                                        // 清空members
                                        members.value = ArrayList()
                                        // 添加成功，跳转回群详情
                                        fragment.requireActivity().findNavController(R.id.app_nav)
                                            .navigateUp()
                                        Toast.makeText(fragment.requireActivity(), "添加成功", Toast.LENGTH_SHORT).show()
                                    } else {
                                        Toast.makeText(fragment.requireActivity(), "添加失败", Toast.LENGTH_SHORT).show()
                                    }
                                }
                            }
                        }
                    }
                }
        }
    }

    fun getMemberById(
        publicViewModel: PublicViewModel,
        memberId: Int,
        fragment: GroupAddMemberFragment
    ) {
        publicViewModel.apply {
            request(MemberAPI::class.java).getMemberById(memberId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val memberResponse = it.data?.data
                                if (memberResponse != null) {
                                    val list = members.value
                                    list!!.add(memberResponse)
                                    members.value = list
                                } else {
                                    Toast.makeText(fragment.requireContext(), "添加失败，不存在此ID", Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}